package org.yoao.crm.struts.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.yoao.crm.business.impl.SignupServiceImpl;
import org.yoao.crm.business.service.ISignupService;
import org.yoao.crm.struts.form.SignupForm;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-8-13 下午09:33:18
 */
public class SignupServlet extends HttpServlet {
	
	public SignupServlet(){
		super();
	}
	
	@Override
	public void init() throws ServletException {
		
	}
	
	@Override
	public void destroy() {
		super.destroy();		
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	
	private ISignupService signupService = new SignupServiceImpl();
	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
		String result = null;
		PrintWriter out = null;
		try {
			request.setCharacterEncoding("GBK");
			response.setCharacterEncoding("GBK");
			out = response.getWriter();
			SignupForm signUpForm = new SignupForm();
			String tableName = request.getParameter("tn");
			if(tableName == null || tableName.trim().equals("")) tableName = "signup"; 
			signUpForm.setAddress(request.getParameter("address"));
			signUpForm.setEmail(request.getParameter("email"));
			signUpForm.setName(request.getParameter("name"));
			signUpForm.setSj(request.getParameter("sj"));
			signUpForm.setIntro(request.getParameter("intro"));
			
			if(signUpForm.getName() == null || signUpForm.getName().trim().equals("")){
				result = "<script>window.alert('信息填写不完整!');document.location='javascript:history.go(-1);';</script>";
				return;
			}
			
			if(signupService.signup(signUpForm, tableName))
				result = "<script>window.alert('恭喜您已经成功报名!');document.location='javascript:history.go(-1);';</script>";
			else 
				result = "<script>window.alert('报名失败，请稍候再试!');document.location='javascript:history.go(-1);';</script>";
		} catch (Exception e) {
			result = "<script>window.alert('报名失败，请稍候再试!');document.location='javascript:history.go(-1);';</script>";
			e.printStackTrace();
		} finally{
			out.println(result);
			out.flush();
			out.close();			
		}
	}
}
